package com.tsingtree.modular.controller.view;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsingtree.base.auth.annotion.Permission;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanSellerDevice;
import com.tsingtree.modular.entity.NanSellerTag;
import com.tsingtree.modular.entity.NanUser;
import com.tsingtree.modular.model.params.NanSellerTagParam;
import com.tsingtree.modular.service.NanSellerDeviceService;
import com.tsingtree.modular.service.NanSellerTagService;
import com.tsingtree.modular.service.NanUserService;
import com.tsingtree.roses.core.base.controller.BaseController;
import com.tsingtree.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 运营组用户分组控制器
 *
 * @author jagger
 */
@Controller
@RequestMapping("/nanSellerTag")
public class NanSellerTagController extends BaseController {

    private String PREFIX = "/nanSellerTag";

    @Autowired
    private NanSellerTagService nanSellerTagService;
    @Autowired
    private NanUserService nanUserService;
    @Autowired
    private NanSellerDeviceService nanSellerDeviceService;

    /**
     * 跳转到主页面
     *
     * @author jagger
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/nanSellerTag.html";
    }

    /**
     * 新增页面
     *
     * @author jagger
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/nanSellerTag_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jagger
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/nanSellerTag_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jagger
     */
    @RequestMapping("/addItem")
    @Permission
    @ResponseBody
    public ResponseData addItem(NanSellerTagParam nanSellerTagParam) {
        long count = nanSellerTagService.count(new QueryWrapper<NanSellerTag>()
                .eq("is_del", false)
                .eq("name", nanSellerTagParam.getName()));
        if(count > 0){
            return ResponseData.error("该标签名称已经存在");
        }
        this.nanSellerTagService.add(nanSellerTagParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jagger
     */
    @RequestMapping("/editItem")
    @Permission
    @ResponseBody
    public ResponseData editItem(NanSellerTagParam nanSellerTagParam) {
        long count = nanSellerTagService.count(new QueryWrapper<NanSellerTag>()
                        .ne("id", nanSellerTagParam.getId())
                        .eq("is_del", false)
                .eq("name", nanSellerTagParam.getName()));
        if(count > 0){
            return ResponseData.error("该标签名称已经存在");
        }
        this.nanSellerTagService.update(nanSellerTagParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jagger
     */
    @RequestMapping("/delete")
    @Permission
    @ResponseBody
    public ResponseData delete(NanSellerTagParam nanSellerTagParam) {
        NanSellerTag byId = nanSellerTagService.getById(nanSellerTagParam.getId());
        byId.setIsDel(true);
        List<NanUser> nanUsers = nanUserService.list(new QueryWrapper<NanUser>().like("tag", "-" + byId.getId() + "-"));
        nanUsers.forEach(nanUser -> {
            String[] split = nanUser.getTag().split(",");
            List<String> res = new ArrayList<>();
            Arrays.stream(split).forEach(tag -> {
                if(!("-" + byId.getId() + "-").equals(tag)){
                    res.add(tag);
                }
            });
            nanUser.setTag(String.join(",", res));
        });
        List<NanSellerDevice> nanSellerDevices = nanSellerDeviceService.list(new QueryWrapper<NanSellerDevice>().like("tag", "-" + byId.getId() + "-"));
        nanSellerDevices.forEach(nanSellerDevice -> {
            String[] split = nanSellerDevice.getTag().split(",");
            List<String> res = new ArrayList<>();
            Arrays.stream(split).forEach(tag -> {
                if(!("-" + byId.getId() + "-").equals(tag)){
                    res.add(tag);
                }
            });
            nanSellerDevice.setTag(String.join(",", res));
        });
        this.nanSellerTagService.remove(byId, nanUsers, nanSellerDevices);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jagger
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NanSellerTagParam nanSellerTagParam) {
        NanSellerTag detail = this.nanSellerTagService.getById(nanSellerTagParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jagger
     */
    @ResponseBody
    @Permission
    @RequestMapping("/list")
    public LayuiPageInfo list(NanSellerTagParam nanSellerTagParam) {
        return this.nanSellerTagService.findPageBySpec(nanSellerTagParam);
    }

}


